package com.bw.shop.frame;

import android.os.Bundle;
import android.widget.RadioGroup;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.LogUtils;
import com.bw.car_module.carlist.CarFragment;
import com.bw.common.providerservice.CheckLoginApi;
import com.bw.goods_module.category.CategoryFragment;
import com.bw.goods_module.home.HomeFragment;
import com.bw.shop.R;
import com.bw.user_module.user.UserFragment;

@Route(path = "/app/frameactivity")
public class FrameActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private HomeFragment homeFragment;//首页fragment
    private CategoryFragment categoryFragment;//分类fragment
    private CarFragment carFragment;//购物车fragment
    private UserFragment userFragment;//用户信息fragment
    private RadioGroup frame_bottom_radio_group;
    public CheckLoginApi checkLoginApi;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_frame);
        frame_bottom_radio_group = findViewById(R.id.frame_bottom_radio_group);
        frame_bottom_radio_group.setOnCheckedChangeListener(this);
        //设置默认加载首页fragment
        showFragment(R.id.frame_home_radio);
//        checkLoginApi = (CheckLoginApi) ARouter.getInstance()
//                .build("/user/checklogin").navigation();
    }

    //隐藏fragment方法
    private void hideFragment(){
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (homeFragment!=null)
            ft.hide(homeFragment);
        if (carFragment!=null)
            ft.hide(carFragment);
        if (categoryFragment!=null)
            ft.hide(categoryFragment);
        if (userFragment!=null)
            ft.hide(userFragment);
        ft.commit();
    }

    //根据用户点击的id选择要显示的fragment
    private void showFragment(int id){
        //全部隐藏掉全部fragment
        hideFragment();
        //根据id区分
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        switch (id){
            case R.id.frame_home_radio:
                if (homeFragment == null){
                    homeFragment = (HomeFragment) ARouter.getInstance().build("/home/homefragment")
                            .navigation();
                    ft.add(R.id.frame_group,homeFragment);
                }else {
                    ft.show(homeFragment);
                }
                break;
            case R.id.frame_category_radio:
                if (categoryFragment == null){
                    categoryFragment = (CategoryFragment) ARouter.getInstance().build("/home/categoryfragment")
                            .navigation();
                    ft.add(R.id.frame_group,categoryFragment);
                }else {
                    ft.show(categoryFragment);
                }
                break;
            case R.id.frame_car_radio:
                if (carFragment == null){
                    carFragment = (CarFragment) ARouter.getInstance().build("/car/carfragment")
                            .navigation();
                    ft.add(R.id.frame_group,carFragment);
                }else {
                    ft.show(carFragment);
                }
                break;
            case R.id.frame_user_radio:
                if (userFragment == null){
                    userFragment = (UserFragment) ARouter.getInstance().build("/user/userfragment")
                            .navigation();
                    ft.add(R.id.frame_group,userFragment);
                }else {
                    ft.show(userFragment);
                }
                break;

        }
        ft.commit();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        showFragment(checkedId);
    }
}
